package checkers;

import checkers.database.ConnectionPool;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
/**
 *
 * @author artinshahverdian
 */
public class ServletContextListener implements javax.servlet.ServletContextListener {

    private String databaseAddress, databaseName, username, password;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
        databaseAddress = context.getInitParameter("databaseAddress");
        databaseName = context.getInitParameter("databaseName");
        username = context.getInitParameter("databaseUsername");
        password = context.getInitParameter("databasePassword");
        try {
            ConnectionPool.createInstance(databaseAddress, databaseName, username, password);
        }
        catch (SQLException ex) {
            Logger.getLogger(ServletContextListener.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (ClassNotFoundException ex) {
            Logger.getLogger(ServletContextListener.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        try {
            ConnectionPool.getInstance().shutDown();
        }
        catch (Exception ex) {
            Logger.getLogger(ServletContextListener.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
